package com.dawnClass.service.impl;

import com.dawnClass.VO.OrderOrderDetailVO;
import com.dawnClass.mapper.ConsumerOrderMapper;
import com.dawnClass.mapper.CourseMapper;
import com.dawnClass.pojo.ConsumerOrder;
import com.dawnClass.pojo.ConsumerOrderExample;
import com.dawnClass.pojo.Course;
import com.dawnClass.service.BuyService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BuyServiceImpl implements BuyService {
    @Autowired
    private ConsumerOrderMapper consumerOrderMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public Course getById(Integer courseId) {
        Course course=courseMapper.selectByPrimaryKey(courseId);
        return  course;
    }

    /**
     * 判断订单是否存在
     * @param consumerId
     * @param courseId
     * @return
     */
    @Override
    public ServiceResponse selectOrder(Integer consumerId, Integer courseId) {
        //查询当前订单是否存在
        ConsumerOrderExample selectExampl2 = new ConsumerOrderExample();
        selectExampl2.createCriteria()
                .andConsumerIdEqualTo(consumerId)
                .andCourseIdEqualTo(courseId)
                .andOrdPayStateEqualTo(2);
        ConsumerOrderExample selectExampl3 = new ConsumerOrderExample();
        selectExampl3.createCriteria()
                .andConsumerIdEqualTo(consumerId)
                .andCourseIdEqualTo(courseId)
                .andOrdPayStateEqualTo(3);
        List<ConsumerOrder> consumerOrders2 = consumerOrderMapper.selectByExample(selectExampl2);
        List<ConsumerOrder> consumerOrders3 = consumerOrderMapper.selectByExample(selectExampl3);
        if (consumerOrders2.size() > 0) {
            System.out.println("未支付:"+consumerOrders2.get(0));
            //该用户存在当前订单记录
            //2未支付
            Course course = courseMapper.selectByPrimaryKey(consumerOrders2.get(0).getCourseId());
            return ServiceResponse.getSuccess(new OrderOrderDetailVO(consumerOrders2.get(0), course));
        }
        if (consumerOrders3.size() > 0) {
            System.out.println("已购买:"+consumerOrders3.get(0));
            //该用户存在当前订单记录
            //3已完成
            return ServiceResponse.getSuccess("已购买");
        }
        return ServiceResponse.getFail("未购买");
    }


    /**
     * 保存订单并且返回
     * @param consumerId
     * @param courseId
     * @param orderTotal
     * @return
     */
    @Override
    public ServiceResponse addOrder(Integer consumerId, Integer courseId, Float orderTotal) {
        Date date = new Date(System.currentTimeMillis());//当前时间
        //创建Orde对象
        ConsumerOrder consumerOrder = new ConsumerOrder();
        consumerOrder.setOrderNum(UUID.randomUUID().toString());
        consumerOrder.setConsumerId(consumerId);
        consumerOrder.setCourseId(courseId);
        consumerOrder.setOrderTotal(orderTotal);
        consumerOrder.setOrderCreationTime(date);
        consumerOrder.setOrdPayState(2);

        int rows = consumerOrderMapper.insert(consumerOrder);
        if(rows >0){
            ConsumerOrderExample consumerOrderExample = new ConsumerOrderExample();
            consumerOrderExample.createCriteria().andOrderNumEqualTo(consumerOrder.getOrderNum());
            List<ConsumerOrder> consumerOrders = consumerOrderMapper.selectByExample(consumerOrderExample);
            Course course = courseMapper.selectByPrimaryKey(consumerOrders.get(0).getCourseId());
            return  ServiceResponse.saveSuccess(new OrderOrderDetailVO(consumerOrders.get(0),course));
        }
        return ServiceResponse.saveFail(null);

    }
}
